package be.ac.ua.comp.projectGPT.auctioneers;
import be.ac.ua.comp.projectGPT.BiddingPolicies.BiddingPolicy;
import be.ac.ua.comp.projectGPT.auctions.Auction;
import be.ac.ua.comp.projectGPT.biddingAgents.BiddingAgent;
import be.ac.ua.comp.projectGPT.players.Person;
import be.ac.ua.comp.projectGPT.players.Provider;
import be.ac.ua.comp.projectGPT.tuple.Tuple;
import be.ac.ua.comp.projectGPT.types.Type;
import java.util.Vector;

public abstract class Auctioneer<T extends Type> extends Person {
    
    Provider provider;
    Auction<T> auction;
    long biddingRound = 0;
    BiddingPolicy biddingPolicy;
    Tuple<BiddingAgent,Long> highestBidder = null;
    long startingBid;
    protected Vector<BiddingAgent> biddingAgents = new Vector<BiddingAgent>();

    public long getHighestBid() {
        return highestBidder.getSnd();
    }

        public void addBiddingAgent(BiddingAgent biddingAgent) {
        biddingAgents.add(biddingAgent);
    }

        public void setStartingBid(Long bid) {
            this.startingBid = bid;
        }

    public void notifyWinnersAndLosers() {
        for(BiddingAgent agent : biddingAgents) {
            if(!(agent == highestBidder.getFst())) {
                logger.info("Notifying losing bidding agent "+agent.getName());
                agent.result(Boolean.FALSE,highestBidder.getSnd());
            } else {
                logger.info("Notifying winning bidding agent "+agent.getName());
                agent.result(Boolean.TRUE,highestBidder.getSnd());
            }
        }
    }

    public void removeBiddingAgent(BiddingAgent biddingAgent) {
         biddingAgents.remove(biddingAgent);
    }

    public Auctioneer(String name, Auction<T> auction) {
        super(name);
        this.auction = auction;
    }

    public Provider getProvider() {
        return provider;
    }

    public void setBiddingPolicy(BiddingPolicy biddingPolicy) {
        logger.info("Setting bidding policy");
        this.biddingPolicy = biddingPolicy;
    }
    
    public void setProvider(Provider provider) {
        this.provider = provider;
    }

    public Auctioneer(String name, Provider provider, Auction<T> auction) {
        super(name);
        this.provider = provider;
        this.auction = auction;
    }
    
    abstract public void runBiddingRound();
}
